package com.haofengsoft.scce.common.client;

import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.haofengsoft.scce.LoadingDialog;

/**
 * Created by Administrator on 15-3-10.
 */
public class UploadTask extends AsyncTask<String, Integer, VolleyBean> {
    String content;
    private Context context;
    VolleyInterface httpInterface;
    String serverName;
    Map<String, Object> mapParam;
    List<PictureInfo> picList;
    private LoadingDialog loadingDialog;

    public UploadTask(Context context, String content, String serverName, Map<String, Object> mapParam, List<PictureInfo> picList, VolleyInterface httpInterface) {
        this.context = context;
        this.content = content;
        this.serverName = serverName;
        this.mapParam = mapParam;
        this.picList = picList;
        this.httpInterface = httpInterface;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (null != content) {
            loadingDialog = new LoadingDialog(context, content);
        }
    }

    @Override
    protected VolleyBean doInBackground(String... params) {
        VolleyBean modelResult = new VolleyBean();
        HttpClient httpClient = null;
        try {
//            MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
//            int count = picList.size();
//            for (int i = 0; i < count; i++) {
//                PictureInfo pic = picList.get(i);
////                StringBody parType = new StringBody(pic.getPicType(), ContentType.TEXT_PLAIN);
//                // 创建缩略图
////                String file_path = CommonTool.PROJECT_TEMP_PATH + "/" + pic.getPicName();
//                File file = new File(pic.getPicPath());
//                if (file.exists()) {
//                    FileBody fileBody = new FileBody(file);
//                    reqEntity.addPart(pic.getPicName(), fileBody);
//                }
//            }
//
//            ContentType contentType = ContentType.create(HTTP.CONTENT_TYPE, HTTP.UTF_8);
//            for (String key : mapParam.keySet()) {
//                StringBody parType = new StringBody(mapParam.get(key).toString(), contentType);
//                reqEntity.addPart(key, parType);
//            }
//            String url = ConstantConfig.API_URL + serverName;
//            HttpPost httpPost = new HttpPost(url);
//
//            httpPost.setEntity(reqEntity.build());
            HttpParams httpParameters = new BasicHttpParams();
//                httpParameters.setParameter(HTTP.CONTENT_ENCODING, HTTP.UTF_8);
//                httpParameters.setParameter(HTTP.CHARSET_PARAM, HTTP.UTF_8);
//                httpParameters.setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET, HTTP.UTF_8);
//                httpParameters.setParameter(HTTP.CONTENT_TYPE, HTTP.UTF_8);
                    /* 从连接池中取连接的超时时间 */
            ConnManagerParams.setTimeout(httpParameters, 6000000);
            // 连接超时
            HttpConnectionParams.setConnectionTimeout(httpParameters, 6000000);
            //// 超时设置
            HttpConnectionParams.setSoTimeout(httpParameters, 6000000);
            httpClient = new DefaultHttpClient(httpParameters);
            HttpResponse response = null;
//            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                System.out.println(result);
                BackInfo backInfo = JSON.parseObject(result, BackInfo.class);
                if (null == backInfo) {
                    // 返回值异常
                    modelResult.setSuccess(false);
                    modelResult.setMessage("返回值异常");
                } else {
//                    if (1 == backInfo.getStatus()) {
//                        modelResult.setSuccess(true);
//                        modelResult.setContent(backInfo.getMessage());
//                        modelResult.setMessage(backInfo.getMessage());
//                    } else {
//                        modelResult.setSuccess(false);
//                        modelResult.setContent(backInfo.getMessage());
//                        modelResult.setMessage(backInfo.getMessage());
//                    }
                }
            } else {
                modelResult.setSuccess(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            modelResult.setSuccess(false);
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return modelResult;
    }

    @Override
    protected void onPostExecute(VolleyBean model) {
        super.onPostExecute(model);
        if (null != content) {
            loadingDialog.dismiss();
        }
        httpInterface.gainData(model);
    }
}
